package com.vigor.bear.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vigor.bear.web.bean.JsonModel;
import com.vigor.bear.web.bean.QueryModel;
import com.vigor.bear.web.bean.Resource;
import com.vigor.bear.web.bean.Role;
import com.vigor.bear.web.service.ResourceService;
import com.vigor.bear.web.service.RoleService;
import com.vigor.bear.web.utils.DatatableUtils;

@Controller
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService rsv;

	@Autowired
	private ResourceService resv;

	@InitBinder("role")
	public void initRoleBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("role.");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String view(Model model) {
		return "system/role/list";
	}

	@RequestMapping(value = "/toList", method = RequestMethod.GET)
	public String toList() {
		return "system/role/list";
	}

	@RequiresPermissions("role:view")
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(@RequestBody String aoData) {
		QueryModel queryModel = DatatableUtils.getQueryModel(aoData);
		return DatatableUtils.getQueryResult(queryModel, rsv.all(queryModel));
	}

	@RequestMapping(value = "/toCreate", method = RequestMethod.GET)
	public String toCreate(Model model) {
		List<Resource> resources = resv.all();
		model.addAttribute("resources", resources);
		return "system/role/add";
	}

	@RequestMapping(value = "/checkRole", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel checkUsername(@RequestParam("role") String role,
			JsonModel json) {
		int ret = 0;
		Role r = rsv.find(role);
		if (r != null) {
			ret = 1;
		}
		handleJsonModel(json, ret, "查询角色");
		return json;
	}

	@RequiresPermissions("role:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(Role role, Model model) {
		int ret = rsv.create(role);
		if (ret > 0) {
			model.addAttribute("result",
					"{\"success\":\"true\",\"message\":\"创建成功!\",\"url\":\"/role\"}");
		} else {
			model.addAttribute("result",
					"{\"success\":\"false\",\"message\":\"创建失败!\",\"url\":\"/role/toCreate\"}");
		}
		return "common/__result";
	}

	@RequestMapping(value = "/toUpdate/{rid}", method = RequestMethod.GET)
	public String toUpdate(@PathVariable("rid") int rid, Model model) {
		List<Resource> resources = resv.all();
		Role role = rsv.get(rid);
		model.addAttribute("role", role);

		List<Resource> selectedResources = new ArrayList<Resource>();
		for (Resource resource : resources) {
			boolean sign = false;
			for (String reid : role.getResources().split(",")) {
				if (reid.equals(String.valueOf(resource.getReid()))) {
					sign = true;
				}
			}
			if (sign) {
				selectedResources.add(resource);
			}
		}
		model.addAttribute("selectedResources", selectedResources);
		
		resources.removeAll(selectedResources);
		model.addAttribute("noselectResources", resources);

		return "system/role/edit";
	}

	@RequiresPermissions("role:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Role role, Model model) {
		int ret = rsv.update(role);
		if (ret > 0) {
			model.addAttribute("result",
					"{\"success\":\"true\",\"message\":\"更新成功!\",\"url\":\"/role\"}");
		} else {
			model.addAttribute("result",
					"{\"success\":\"false\",\"message\":\"更新失败!\",\"url\":\"/role/toUpdate\"}");
		}
		return "common/__result";
	}

	@RequiresPermissions("role:delete")
	@RequestMapping(value = "/{rid}/delete", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel delete(@PathVariable("rid") int rid, JsonModel json) {
		int ret = rsv.delete(rid);
		handleJsonModel(json, ret, "删除");
		return json;
	}

	@RequiresPermissions("role:deleteSelect")
	@RequestMapping(value = "/deleteSelect", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel deleteSelect(@RequestParam("rids") String rids,
			JsonModel json) {
		int ret = rsv.deleteSelect(rids.split(","));
		handleJsonModel(json, ret, "删除所选");
		return json;
	}

	private void handleJsonModel(JsonModel json, int ret, String op) {
		json.setSuccess(ret > 0);
		json.setMessage(op + (ret > 0 ? "成功" : "失败"));
	}
}
